﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace Sapiens.at.SharePoint.Receiver
{
    /// <summary>
    /// Represents a configuration for an event receiver of the <see cref="BaseItemReceiver"/>.
    /// </summary>
    public class ItemEventConfig
    {
        /// <summary>
        /// Gets or sets the priority of the event. Use this to order events.
        /// </summary>
        public int Priority { get; set; }

        /// <summary>
        /// Gets or sets which event types cause the <see cref="Callback"/> being invoked.
        /// </summary>
        public IEnumerable<SPEventReceiverType> EventTypes { get; set; }

        /// <summary>
        /// Gets or sets which content types cause the <see cref="Callback"/> being invoked.
        /// </summary>
        public IEnumerable<SPContentTypeId> ContentTypeIDs { get; set; }

        /// <summary>
        /// Gets or sets which fields have to change in order to call the <see cref="Callback"/>.
        /// This doesn't work on EventType <see cref="SPEventReceiverType.ItemUpdated"/>
        /// </summary>
        public IEnumerable<string> OnChangedFields { get; set; }

        /// <summary>
        /// If true, all fields defined in <see cref="OnChangedFields"/> have to change in order to call the <see cref="Callback"/>;
        /// otherwise just one of the fields has to change.
        /// </summary>
        public bool AllChangedFieldsMustMatch { get; set; }

        /// <summary>
        /// The method that gets called when the event conditions meet this configuration.
        /// </summary>
        public ItemEventDelegate Callback { get; set; }

        /// <summary>
        /// If true, the <see cref="Callback"/> method gets called in a privileged context.
        /// </summary>
        public bool Privileged { get; set; }

        /// <summary>
        /// Returns a readable string representation for this configuration.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            var sb = new StringBuilder("{");
            sb.AppendFormat("EventTypes: {0} - ", Convert.ToString(EventTypes));
            sb.AppendFormat("ContentTypeIDs: {0} - ", Convert.ToString(ContentTypeIDs));
            sb.AppendFormat("OnChangedFields({0}): {1}", AllChangedFieldsMustMatch ? "All" : "Any", Convert.ToString(OnChangedFields));
            sb.AppendFormat("Callback: {0} - ", Callback.Method.Name);
            if (Privileged) { sb.Append(" - PRIVILEGED"); }
            return sb.Append("}").ToString();
        }
    }
}
